jQuery( function($){

	var searchurl = "https://approvalzoom.com/sphinx/json.zack.php?limit=25&skip=0&q=";
	var listingurl = "https://approvalzoom.com/listing/";
	var searchquery = '';
	
	// Storing the seconds since the epoch in now:
	var now = (new Date()).getTime()/1000;
	var locStorTime = parseInt(localStorage.time);
	var locStorDiff = now - locStorTime;
	
	//console.log( 'now (' + now + ') - locStorTime (' + locStorTime + ') = locStorDiff (' + locStorDiff + ') > ' + (1*60*60) );


// log keystrokes on query input and activate search on enter key
	$('input#zoom-search-query').on('keypress', function( e ){
		//console.log( 'adv search input keypress on key num ' + e.which );
		if( e.which == 13 ){ e.preventDefault(); $('#zoom-search-button').trigger('click'); }
	});	
	
	
	
	$('#zoom-search-button').on( 'click', function(){
			var q = $('#zoom-search-query').val();
			searchquery = encodeURIComponent( q );
			$('#zoom-webapp-content').html('<div id="zoom-search-loading">LOADING RESULTS...</div>');
			//console.log('zoom search extension initiated');
			//console.log(now - parseInt(localStorage.time));
			//console.log( 'query: ' + searchquery );

		// If there is no cache set in localStorage, or the cache is older than 1 hour:
		//if( !localStorage.cache ||  locStorDiff > (1*60*60) )
		if( !localStorage.cache || localStorage.q != q  )
		{
			//console.log( 'no cache available... running new query' );
						
						
			var url=searchurl+""+searchquery;
			//console.log( 'url: ' + url );
			
			
			$.get( url , function( d ){
				// msg.query.results.item is an array:
				var items = d.matches;
				var htmlString = "";
				if( items && items.length>0 ){
					for(var i=0;i<items.length;i++)
					{
						var zoom = items[i].data;
						$.extend(zoom,items[i].attr);
						// Extracting the post ID from the permalink:
						//var id = zoom.guid.content.match(/(\d+)$/)[0];
						
						
						
						
						// Looping and generating the markup of the tutorials:
						
						htmlString += '<div class="zoom-result">\
										<a class="zoom-result-title" target="approvalzoom" href="'+listingurl+zoom.title+'">'+zoom.title+'</a><br />\
										by:	<a class="zoom-result-mfr" target="approvalzoom" href="https://approvalzoom.com'+zoom.company_url+'">' + zoom.manufacturer + '</a>\
										<p>'+zoom.description+'</p></div>\
										';
					}
				}else{
					htmlString = 'Your search returned 0 results.';
				}
				// Setting the cache
				localStorage.cache	= htmlString;
				localStorage.time	= now;
				localStorage.q 		= q;
				// Updating the content div:

				$('#zoom-webapp-content').html( htmlString );
			},'json');
		}
		else
		{
			//console.log( 'cache called' );
			// The cache is fresh, use it:
			$('#zoom-webapp-content').html( localStorage.cache );
		}
		setTimeout( "jQuery('input#zoom-search-query').focus();",2000);
		
	});
	
	//$('#zoom-search-button').trigger('click');
	
	if( localStorage.cache ){
		$('#zoom-search-query').val( localStorage.q );
		$('#zoom-webapp-content').html( localStorage.cache );
	}

	
	setTimeout( "jQuery('input#zoom-search-query').focus();",2000);
	
});

setTimeout( "jQuery('input#zoom-search-query').focus();",2000);